package org.jsbd.boss.mq;

import java.util.Iterator;
import java.util.Set;

import org.jsbd.boss.bto.HttpResult;
import org.jsbd.boss.domian.statistics.BaseDTO;

import com.alibaba.fastjson.JSONObject;

public class UserLogManager {
	
	private static final String TYPE="type";
	private static final String TOPIC_NAME="userReqLog";
	
	public static final String TYPE_REQ="userReq";
	public static final String TYPE_THIRD_S="thirdSucc";
	public static final String TYPE_BIZ_S="bizSucc";
	public static final String TYPE_SAVEUSER="saveUser";
	public static final String TYPE_NEWUSER="newUser";
	public static final String TYPE_HAS_DO="hasDo";
	public static final String TYPE_SDK_F="sdkFile";
	public static final String TYPE_SO_F="soFile";
	
    public static void addUserReq(HttpResult httpResult) {
    	JSONObject json=getJsonObject(httpResult);
    	json.put(TYPE, TYPE_REQ);
    	addToKafka(json);
    }
    
    public static void addThirdSuccLog(HttpResult httpResult) {
    	JSONObject json=getJsonObject(httpResult);
    	json.put(TYPE,TYPE_THIRD_S);
    	addToKafka(json);
    }
    
    public static void addBizSuccLog(HttpResult httpResult) {
    	JSONObject json=getJsonObject(httpResult);
    	json.put(TYPE,TYPE_BIZ_S);
    	addToKafka(json);
    }
    
    public static void addSaveUserSuccLog(HttpResult httpResult) {
    	JSONObject json=getJsonObject(httpResult);
    	json.put(TYPE,TYPE_SAVEUSER);
    	addToKafka(json);
    }
    
    public static void addUserHasDoLog(Set<Integer> hasDoSet,HttpResult httpResult) {
    	JSONObject json=getJsonObject(httpResult);
    	json.put(TYPE,TYPE_HAS_DO);
    	StringBuffer buffer=new StringBuffer();
    	Iterator<Integer> baseIt=hasDoSet.iterator();
    	while(baseIt.hasNext()) {
    		buffer.append(baseIt.next()).append(",");
    	}
    	
    	if(buffer.length()>0) {
    		buffer=buffer.deleteCharAt(buffer.length()-1);
    	}
    	json.put("hasDo", buffer.toString());
    	addToKafka(json);
    }
    
    public static void addSdkFileSuccLog(String uuid) {
    	JSONObject json=new JSONObject();
    	json.put("uuid", uuid);
    	json.put(TYPE, TYPE_SDK_F);
    	addToKafka(json);
    }
    
    public static void addSoFileSuccLog(String uuid) {
    	JSONObject json=new JSONObject();
    	json.put("uuid", uuid);
    	json.put(TYPE, TYPE_SO_F);
    	addToKafka(json);
    }
    
    private static JSONObject getJsonObject(HttpResult httpResult) {
    	JSONObject json=new JSONObject();
    	json.put("uuid", httpResult.getUuid());
    	json.put("imsi", httpResult.getImsi());
    	json.put("sdkVerName", httpResult.getSdkVerName());
    	return json;
    }
    
    private static void addToKafka(JSONObject json){
    	BaseDTO<Object> data=new BaseDTO<>();
    	data.setData(json.toJSONString());
    	data.setTopic(TOPIC_NAME);
    	KafkaMqManager.saveDtoToQueue(data);
    }
	
}
